import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { CmtContentCommentApi } from '#/api/cmt/content/comment';

import { getCommentOptions } from '#/api/cmt/content/comment';
import { getBaseCommunityOptions } from '#/api/cmt/base/community';
import { getMerchantInfoOptions } from '#/api/cmt/base/merchantInfo';
import { getRangePickerDefaultProps } from '#/utils';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'communityId',
      label: '社区',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择社区',
        api: getBaseCommunityOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'merchantId',
      label: '商户',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择商户',
        api: getMerchantInfoOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'targetType',
      label: '目标类型',
      rules: 'required',
      component: 'Select',
      formItemClass: 'col-span-1',
      componentProps: {
        options: [
          { label: '帖子', value: 'post' },
          { label: '活动', value: 'activity' },
          { label: '课程', value: 'course' },
        ],
        placeholder: '请选择评论目标类型',
      },
    },
    {
      fieldName: 'targetId',
      label: '目标ID',
      rules: 'required',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入评论目标ID',
      },
    },
    {
      fieldName: 'userId',
      label: '评论会员',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入评论会员ID',
      },
    },
    {
      fieldName: 'parentId',
      label: '父评论',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择父评论',
        api: getCommentOptions,
        resultField: 'list',
        labelField: 'content',
        valueField: 'id',
      },
    },
    {
      fieldName: 'replyToUserId',
      label: '回复会员',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入回复会员ID',
      },
    },
    {
      fieldName: 'content',
      label: '评论内容',
      rules: 'required',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 4,
        placeholder: '请输入评论内容',
      },
    },
    {
      fieldName: 'images',
      label: '图片',
      component: 'ImageUpload',
      formItemClass: 'col-span-2',
      componentProps: {
        multiple: true,
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'targetType',
      label: '目标类型',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: [
          { label: '帖子', value: 'post' },
          { label: '活动', value: 'activity' },
          { label: '课程', value: 'course' },
        ],
        placeholder: '请选择目标类型',
      },
    },
    {
      fieldName: 'targetId',
      label: '目标ID',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入目标ID',
      },
    },
    {
      fieldName: 'userId',
      label: '评论会员',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入评论会员ID',
      },
    },
    {
      fieldName: 'auditStatus',
      label: '审核状态',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: [
          { label: '待审核', value: 0 },
          { label: '通过', value: 1 },
          { label: '拒绝', value: 2 },
        ],
        placeholder: '请选择审核状态',
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        clearable: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<CmtContentCommentApi.Comment>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '评论ID',
      minWidth: 100,
    },
    {
      field: 'targetType',
      title: '目标类型',
      minWidth: 100,
      slots: { default: 'targetType' },
    },
    {
      field: 'targetId',
      title: '目标ID',
      minWidth: 100,
    },
    {
      field: 'userId',
      title: '评论会员',
      minWidth: 100,
    },
    {
      field: 'content',
      title: '评论内容',
      minWidth: 250,
    },
    {
      field: 'parentId',
      title: '父评论ID',
      minWidth: 100,
    },
    {
      field: 'replyToUserId',
      title: '回复会员',
      minWidth: 100,
    },
    {
      field: 'likeCount',
      title: '点赞数',
      minWidth: 100,
    },
    {
      field: 'auditStatus',
      title: '审核状态',
      minWidth: 100,
      slots: { default: 'auditStatus' },
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 160,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
